package com.controller;

import com.pojo.Admin;
import com.pojo.AdminLoginParam;
import com.pojo.RespBean;
import com.server.IAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;

/**
 * @author lvjiazhen
 * @date 2021/7/22  15:53
 */
@RestController
public class LoginController {

    @Autowired
    private IAdminService adminService;

    @PostMapping("/login")
    public RespBean login(AdminLoginParam adminLoginParam, HttpServletRequest request){
        return adminService.login(adminLoginParam.getUsername(),adminLoginParam.getPassword(),adminLoginParam.getCode(),request);
    }

    @PostMapping("/loginOut")
    public RespBean loginOut(){
        return RespBean.success("注销成功");
    }

    @PostMapping("/admin/info")
    public Admin getAdminInfo(Principal principal){
        if(principal == null){
            return null;
        }
        String username = principal.getName();
        Admin admin = adminService.getAdminByUserName(username);
        admin.setPassword(null);
        return admin;
    }
}
